package service.Impl;

import dao.mapper.OrderMapper;
import org.apache.ibatis.session.SqlSession;
import service.IOrderService;
import utils.SqlSessionUtil;
import vo.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class IOrderServiceImpl implements IOrderService {
    SqlSession sqlSession=new SqlSessionUtil().sqlSession;
    OrderMapper orderMapper=sqlSession.getMapper(OrderMapper.class);

    ICartLineServiceImpl cartLineService=new ICartLineServiceImpl();
    IOrderLineServiceImpl orderLineService=new IOrderLineServiceImpl();
    IOrderStateServiceImpl orderStateService=new IOrderStateServiceImpl();
    ICartServiceImpl cartService=new ICartServiceImpl();
    IBookServiceImpl bookService=new IBookServiceImpl();
    @Override
    public List<OrderLine> addOrder(List<CartLine>cartLines, int userId) {
        //添加一条userOrder数据

        List<OrderLine>orderLines=new ArrayList<>();


        double totalPrice=cartLineService.totalPrice(cartLines);
        Date date=new Date();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        Timestamp time=Timestamp.valueOf(nowTime);
        Order order=new Order();
        order.setDate(time);
        order.setTotalPrice(totalPrice);
        order.setUserId(userId);
        orderMapper.addOrder(order);
        int id=order.getId();
        sqlSession.commit();

        Order order1=orderMapper.selectOrderById(id);
        int orderId=order1.getId();

        //在orderLine中添加新的数据  //在用户购物车中减少对应购物记录

        for(CartLine cartLine:cartLines){
            int bookId=cartLine.getBookId();
            int quanlity=cartLine.getQuanlity();

            orderLineService.addOrderLine(orderId,bookId,quanlity);

            OrderLine orderLine=orderLineService.selectOrderLine(orderId,bookId);
            orderLines.add(orderLine);

            //删除购物车中的该数据
           cartLineService.delCartLineById(cartLine.getId());
        }

        //在orderState中添加新数据

        orderStateService.addOrderState(orderId);

        sqlSession.commit();

       //在购物车中减少相应的价钱

        int cartId=cartService.selectCart(userId).getId();
        List<CartLine>cartLines1=cartLineService.selectCartLineByCartId(cartId);
        double totalMoney=cartLineService.totalPrice(cartLines1);
        cartService.changeTotalPrice(cartId,totalMoney);

        return orderLines;
    }

    @Override
    public double totalPrice(List<OrderLine> orderLines)
    {
        double total=0.00;
        for(OrderLine orderLine:orderLines){
            int num=orderLine.getQuanlity();
            int bookID=orderLine.getBookId();
            Book book=bookService.selectBookById(bookID);
            double price=book.getPrice();
            total=total+(price*num);
        }
        return total;
    }

    @Override
    public List<Order> selectOrdersByUserId(int userId) {
        return orderMapper.selectOrderByUserId(userId);
    }

    @Override
    public Order getOrderById(int id) {
        Order order = orderMapper.selectOrderById(id);
        return order;
    }
}
